<?php

/**
 * 商品相关 - RPC
 * 
 * @author birdy <freebirdy@foxmail.com>
 * @link http://www.aiyuangong.com
 */

namespace app\seller\rpc;

class Goods extends \app\common\rpc\Rpc
{
    /**
     * 出售中的商品
     * 
     * @return []
     */
    public function online($where = [], $page, $limit = 10)
    {
        $condition = $where;
        $condition['goods_state']   = 1;
        $condition['goods_verify']  = 1;
        $total = \think\Db::name('goods_common')->where($condition)->count();
        if ($total > 0) {
            $goods_list = model('goods')->getGoodsCommonOnlineList($where, '*', $page, $limit);
            $storage = model('goods')->calculateStorage($goods_list); // 计算库存
        } else {
            $goods_list = [];
            $storage    = [];
        }
        
        return [
            'total' => $total,
            'goods_list' => $goods_list,
            'storage' => $storage
        ];
    }
    
    
    /**
     * 出售中的商品
     * 
     * @return []
     */
    public function offline($where = [], $page, $limit = 10)
    {
        $condition = $where;
        $condition['goods_state']   = 0;
        $condition['goods_verify']  = 1;
        $total = \think\Db::name('goods_common')->where($condition)->count();
        if ($total > 0) {
            $goods_list = model('goods')->getGoodsCommonOfflineList($where, '*', $page, $limit);
            $storage = model('goods')->calculateStorage($goods_list); // 计算库存
        } else {
            $goods_list = [];
            $storage    = [];
        }
        
        return [
            'total' => $total,
            'goods_list' => $goods_list,
            'storage' => $storage
        ];
    }
    
    /**
     * 更新贷品信息
     * @param type $where
     * @param type $data
     */
    public function update($where = [], $data = [])
    {
        return model('goods')->editProducesNoLock($where, $data);
    }
    
    /**
     * 删除商品
     */
    public function delete($store_id, $commonid)
    {
        if (!is_array($commonid)) {
            $commonid = explode(',', $commonid);
        }
        $condition = [
            'store_id' => $store_id,
            'id' => ['in', $commonid]
        ];
        return model('goods')->delGoodsNoLock($condition);
    }

    /**
     * 获取商品列表
     * @param type $condition
     * @param type $field
     * @param type $group
     * @param type $order
     * @param type $page
     * @param type $limit
     * @param type $lock
     * @return type
     */
    public function lists($condition, $field = '*', $group = '', $order = 'id desc', $page = 1, $limit = 99999, $lock = false)
    {
        return model('goods')->getGoodsList($condition, $field, $group, $order, $page, $limit, $lock);
    }
    
    /**
     * 下架
     * @param type $condition
     * @return type
     */
    public function unshow($store_id, $commonid)
    {
        $condition = [
            'store_id' => $store_id,
            'id' => ['in', $commonid]
        ];
        return model('goods')->editProducesOffline($condition);
    }
    
    /**
     * 上架
     * @param type $condition
     * @return type
     */
    public function show($store_id, $commonid)
    {
        $condition = [
            'store_id' => $store_id,
            'id' => ['in', $commonid]
        ];
        return model('goods')->editProducesOnline($condition);
    }

}
